package com.decoration.basic.util;

import com.alibaba.druid.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    /**
     * 脱敏数据处理
     * @param param
     * @return
     */
    private static String replaceStr(String param){
        int len = param.length();
        if(isContainChinese(param)){
            return param.replaceAll("(.{1}(.)(.{0}))","1"+"**"+"3");
        }
        return param.replaceAll("(.{"+(len <12?3:6)+"})(.*)(.{4})","1"+"****"+"3");
    }

    /**
     *  判断字符串中是否含中文，是返回ture，否返回false
     */
    public static boolean isContainChinese(String str){
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if(m.find()){
            return true;
        }
        return false;
    }

    /**
     *  数组转换成String字符串
     */
    public static String arrayAsStr(List<String> args){
        StringBuffer result = new StringBuffer();
        int i = 1;
        for (String list : args){
            result.append(list);
            if (i == args.size()){
                break;
            }
            result.append(",");
            i++;
        }
        return result.toString();
    }

    public static boolean isNotEmpty(String cs) {
        return !isEmpty(cs);
    }

    public static boolean isEmpty(String str) {
        if (str == null) {
            return true;
        } else {
            String testStr = trim(str);
            return testStr.length() == 0;
        }
    }

    public static boolean isEmpty(Object str) {
        if (str == null) {
            return true;
        } else {
            String testStr = trim(str.toString());
            return testStr.length() == 0;
        }
    }

    public static String trim(String str) {
        return str != null ? str.trim() : str;
    }

    /**
     *  String转换成数组字符串
     */
    public static List<String> StrAsArray(String args){
        //校验数据
        if (StringUtils.isEmpty(args)){
            return null;
        }

        List<String> result = new ArrayList<>();
        String[] splstr= args.split(",");
        for (int i = 0;i < splstr.length ; i++ ){
            result.add(splstr[i]);
        }
        return result;
    }

    class Solution{
        public int[] sortArray(int[] nums){
            if (nums == null || nums.length <= 1){
                return nums;
            }
            quickSort(nums,0,nums.length);
            return nums;
        }

        private void quickSort(int[] nums,int start,int end){
            if (start >= end){return;}
            int middle = partition(nums,start,end);
            quickSort(nums,start,middle -1);
            quickSort(nums,middle+1,end);
        }
        private int partition(int[] nums,int start,int end){
            int pivot = nums[start];
            int left = start + 1;
            int right = end;
            while (left < right){
                while (left < right && nums[left] <= pivot){
                    ++left;
                }
                while (left < right && nums[left] >= pivot){
                    --right;
                }
                if(left < right){
                    swap(nums,left,right);
                    ++left;
                    --right;
                }
            }
            if (left == right && nums[right] > pivot) {--right;}
            swap(nums,start,right);
            return right;
        }
        private void swap(int[] nums,int left,int right){
            if (nums == null || nums.length <= 1){return;}
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
        }
    }

    public  static void main(String[] args){
        String name = "李武";
        System.out.println(isContainChinese(name));
        System.out.println(replaceStr(name));
        String id = "551254198807213368";
        System.out.println(isContainChinese(id));
        System.out.println(replaceStr(id));
        String bankId = "6225337608836806";
        System.out.println(isContainChinese(bankId));
        System.out.println(replaceStr(bankId));
    }
}
